﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Jiuyong
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
			Loaded += MainWindow_Loaded;
        }

		void MainWindow_Loaded(object sender, RoutedEventArgs e)
		{
			this.CommandBindings.Add(new CommandBinding(ApplicationCommands.New,ViewModel.OpenTool));
		}

		public ViewModels.MainWindowViewModel ViewModel
		{
			get
			{
				return DataContext as ViewModels.MainWindowViewModel;
			}
		}
    }

    public class NameTypeDictionary : Dictionary<string, Type>, INotifyCollectionChanged, INotifyPropertyChanged
    {

        public event NotifyCollectionChangedEventHandler CollectionChanged;

        public event PropertyChangedEventHandler PropertyChanged;

    }

    namespace ViewModels
    {
        public class MainWindowViewModel : ViewModelBase
        {
			public MainWindowViewModel()
			{
				Register<Views.RehabFilePage>("文件康复");
				Register<Views.SuperReplacePage>("超级替换");
                Register<Views.CodingModelsPage>("自动建模");
			}

			public Page Create(string capition)
			{
				var t = _tools[capition];
				var c = t.GetConstructor(new Type[0]);
				var p = c.Invoke(new Type[0]) as Page;
				return p;
			}

			public void Register<T>(string capition)
				where T : Page
			{
				_tools.Add(capition, typeof(T));
			}

			public void OpenTool(object sender, ExecutedRoutedEventArgs e)
			{
				var t = e.Parameter as Type;
				var c = t.GetConstructor(new Type[0]);
				var p = c.Invoke(new Type[0]) as Page;

				var win = new Window()
				{
					Content = p
				};
				var mw = App.Current.MainWindow;
				win.Closed += (p1, p2) =>
				{
					mw.Show();
				};
				win.Show();
				mw.Hide();
			}

			private NameTypeDictionary _tools = new NameTypeDictionary();
			public NameTypeDictionary Tools
			{
				get
				{
					return _tools;
				}

				set
				{
					_tools = value;
					OnPropertyChanged("Tools");
				}
			}
        }
    }
}
